package me.iwf.PhotoPickerDemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;

import java.io.Serializable;
import java.util.UUID;

import me.geolo.photopicker.adapter.PhotoGridAdapter;
import me.geolo.photopicker.adapter.PhotoPagerAdapter;
import me.geolo.photopicker.adapter.PopupDirectoryListAdapter;

/**
 * Created by geolo on 2017/12/12.
 */

public class GlideImgLoadRequest implements Serializable {

    private RequestManager mRequestManager;

    public void init(Context context) {
        mRequestManager = Glide.with(context);
    }

    public void onSetup(Class destClass, Object path, ImageView imageView) {
        onSetup(destClass, path, 800, imageView);
    }

    public void onSetup(Class destClass, Object path, float imageSize, ImageView imageView) {
        if (destClass.equals(PhotoGridAdapter.class)) {
            mRequestManager.setDefaultRequestOptions(new RequestOptions().centerCrop()
                .dontAnimate()
                .override((int) imageSize, (int) imageSize)
                .placeholder(R.drawable.__picker_ic_photo_black_48dp)
                .error(R.drawable.__picker_ic_broken_image_black_48dp)).load(path).thumbnail(0.5f).into(imageView);
        } else if (destClass.equals(PhotoPagerAdapter.class)) {
            mRequestManager.setDefaultRequestOptions(new RequestOptions().dontAnimate()
                .dontTransform()
                .override(800, 800)
                .placeholder(R.drawable.__picker_ic_photo_black_48dp)
                .error(R.drawable.__picker_ic_broken_image_black_48dp)).load(path).thumbnail(0.1f).into(imageView);

        } else if (destClass.equals(PopupDirectoryListAdapter.class)) {
            mRequestManager.setDefaultRequestOptions(new RequestOptions().dontAnimate()
                .dontTransform()
                .override(800, 800)
                .placeholder(R.drawable.__picker_ic_photo_black_48dp)
                .error(R.drawable.__picker_ic_broken_image_black_48dp)).load(path).thumbnail(0.1f).into(imageView);
        } else {
            mRequestManager.setDefaultRequestOptions(new RequestOptions().dontAnimate()
                .dontTransform()
                .override(800, 800)
                .placeholder(R.drawable.__picker_ic_photo_black_48dp)
                .error(R.drawable.__picker_ic_broken_image_black_48dp)).load(path).thumbnail(0.1f).into(imageView);
        }
    }

    public void onDownload(Class destClass, final Context context, Object path, final boolean[] isDownloading_0,
        final View progressLayout) {
        String url = path.toString();
        mRequestManager.asBitmap().load(Uri.parse(url)).listener(new RequestListener<Bitmap>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object o, Target<Bitmap> target, boolean b) {
                if (progressLayout != null) {
                    progressLayout.setVisibility(View.GONE);
                }
                if (e != null) {
                    e.printStackTrace();
                }
                isDownloading_0[0] = false;
                return false;
            }

            @Override
            public boolean onResourceReady(Bitmap bitmap, Object o, Target<Bitmap> target, DataSource dataSource,
                boolean b) {
                Log.v("geolo", "geolo img success ");
                saveBmp(context, bitmap);
                if (progressLayout != null) {
                    progressLayout.setVisibility(View.GONE);
                }
                isDownloading_0[0] = false;
                return false;
            }
        }).submit();
    }

    public void resumeRequests() {
        if (mRequestManager != null) {
            mRequestManager.resumeRequests();
        }
    }

    public void pauseRequests() {
        if (mRequestManager != null) {
            mRequestManager.pauseRequests();
        }
    }

    public void clear(View view) {
        if (mRequestManager != null) {
            mRequestManager.clear(view);
        }
    }

    private void saveBmp(Context context, Bitmap resource) {
        if (resource != null && !resource.isRecycled()) {
            String downloadPath = MediaStore.Images.Media.insertImage(context.getContentResolver(), resource,
                UUID.randomUUID().toString(), "geolo_moto");
            if (!TextUtils.isEmpty(downloadPath)) {
                Toast.makeText(context, "图片已保存到相册", Toast.LENGTH_SHORT).show();
                Log.v("geolo", "geolo img url : " + downloadPath);
            }
        }
    }

}
